package com.ls.userlog.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ls.userlog.entity.User;
import com.ls.userlog.dao.UserLogMapper;
import com.ls.userlog.service.UserOperateLogService;
import com.ls.userlog.utils.EqualUtils;
import com.ls.userlog.utils.IPUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

@Service
public class UserOperateLogServiceImpl implements UserOperateLogService {

    @Autowired
    private UserLogMapper userLogMapper;


    @Override
    public Integer insert(HttpServletRequest request) {
        User user = new User();
        int result = 0;
        //获取用户发起的请求路径url
        String requestURI = request.getRequestURI();
        if(StringUtils.isNotBlank(requestURI)){
            //获取对应的操作名称
            String operateName = EqualUtils.equal(requestURI);
            //获取用户ip
            String requestIp = IPUtil.getRequestIp(request);
            //生成的是不带-的字符串，类似于：b17f24ff026d40949c85a24f4f375d42
            String uuid = IdUtil.simpleUUID();
            user.setId(uuid);
            user.setName(operateName);
            user.setIp(requestIp);
            user.setUrl(requestURI);
            result = userLogMapper.insert(user);
        }
        return result;
    }

    @Override
    public User select(String id) {
        User result = userLogMapper.select(id);
        return result;
    }

    @Override
    public Integer add(User user) {
        int result = userLogMapper.insert(user);
        return result;
    }
}
